function inZone(id,x1,x2,y1,y2)
    if (player(id,"tilex")>=x1 and player(id,"tilex")<=x2 and player(id,"tiley")>=y1 and player(id,"tiley")<=y2) then
        return true
    end
end

function test(id)
    for zz=1,#zones do
        if (inZone(id,zones[zz].xs,zones[zz].xe,zones[zz].ys,zones[zz].ye)) then
            hudtxt2(id,27,"000255000","You are in "..zones[zz].name.." sector, "..factions[zones[zz].faction].name.." space",120,20)
			break
        else
            hudtxt2(id,27,"000255000","You are in open space",120,20)
        end
    end
end

addhook("movetile","zonemove")
function zonemove(id,x,y)
    test(id)
end

function getComm(b,n)
    if (basedata[b]~=nil and basedata[b].commtr[n]~=nil) then
        return comms[basedata[b].commtr[n].type].name.."|"..basedata[b].commtr[n].price
    else
        return ""
    end
end

function getBComm(b,n)
    if (basedata[b]~=nil and basedata[b].commb[n]~=nil) then
        return comms[basedata[b].commb[n].type].name.."|"..basedata[b].commb[n].price
    else
        return ""
    end
end

function getEq(b,n)
    if (basedata[b] and basedata[b].eqtr[n]) then
        return equipment[basedata[b].eqtr[n].type].name.."|"..basedata[b].eqtr[n].price
    else
        return ""
    end
end

function getShip(b,n)
    if (basedata[b] and basedata[b].shtr[n]) then
        return ships[basedata[b].shtr[n].type].name.."|"..basedata[b].shtr[n].price
    else
        return ""
    end
end

function trademenu(id,z,p)
	p=p or 1
	local pages=math.ceil(#basedata[z].commtr/6)
	if (pages>1) then
		if (pages>2) then
			if (p==1) then
				menu(id,"Commodity Trader "..p.." - "..zones[z].name..","..getComm(z,1)..","..getComm(z,2)..","..getComm(z,3)..","..getComm(z,4)..","..getComm(z,5)..","..getComm(z,6)..",Next,,Sell")
			elseif (p>1 and p<pages) then
				menu(id,"Commodity Trader "..p.." - "..zones[z].name..","..getComm(z,1+(6*(p-1)))..","..getComm(z,2+(6*(p-1)))..","..getComm(z,3+(6*(p-1)))..","..getComm(z,4+(6*(p-1)))..","..getComm(z,5+(6*(p-1)))..","..getComm(z,6+(6*(p-1)))..",Next,Prev,Sell")
			else
				menu(id,"Commodity Trader "..p.." - "..zones[z].name..","..getComm(z,1+(6*(pages-1)))..","..getComm(z,2+(6*(pages-1)))..","..getComm(z,3+(6*(pages-1)))..","..getComm(z,4+(6*(pages-1)))..","..getComm(z,5+(6*(pages-1)))..","..getComm(z,6+(6*(pages-1)))..",,Prev,Sell")
			end
		elseif (pages==2) then
			if (p==1) then
				menu(id,"Commodity Trader "..p.." - "..zones[z].name..","..getComm(z,1)..","..getComm(z,2)..","..getComm(z,3)..","..getComm(z,4)..","..getComm(z,5)..","..getComm(z,6)..",Next,,Sell")
			elseif (p>1 and p<pages) then
				menu(id,"Commodity Trader "..p.." - "..zones[z].name..","..getComm(z,7)..","..getComm(z,8)..","..getComm(z,9)..","..getComm(z,10)..","..getComm(z,11)..","..getComm(z,12)..",,Prev,Sell")
			end
		end
	elseif (pages==1) then
		menu(id,"Commodity Trader "..p.." - "..zones[z].name..","..getComm(z,1)..","..getComm(z,2)..","..getComm(z,3)..","..getComm(z,4)..","..getComm(z,5)..","..getComm(z,6)..",,,Sell")
	end
end

function trsellmenu(id,z,p)
	p=p or 1
	local pages=math.ceil(#basedata[z].commtr/6)
	if (pages>1) then
		if (pages>2) then
			if (p==1) then
				menu(id,"Commodity Trader (Sell) "..p.." - "..zones[z].name..","..getBComm(z,1)..","..getBComm(z,2)..","..getBComm(z,3)..","..getBComm(z,4)..","..getBComm(z,5)..","..getBComm(z,6)..",Next,,Buy")
			elseif (p>1 and p<pages) then
				menu(id,"Commodity Trader "..p.." - "..zones[z].name..","..getBComm(z,1+(6*(p-1)))..","..getBComm(z,2+(6*(p-1)))..","..getBComm(z,3+(6*(p-1)))..","..getBComm(z,4+(6*(p-1)))..","..getBComm(z,5+(6*(p-1)))..","..getBComm(z,6+(6*(p-1)))..",Next,Prev,Buy")
			else
				menu(id,"Commodity Trader "..p.." - "..zones[z].name..","..getBComm(z,1+(6*(pages-1)))..","..getBComm(z,2+(6*(pages-1)))..","..getBComm(z,3+(6*(pages-1)))..","..getBComm(z,4+(6*(pages-1)))..","..getBComm(z,5+(6*(pages-1)))..","..getBComm(z,6+(6*(pages-1)))..",,Prev,Buy")
			end
		elseif (pages==2) then
			if (p==1) then
				menu(id,"Commodity Trader (Sell) "..p.." - "..zones[z].name..","..getBComm(z,1)..","..getBComm(z,2)..","..getBComm(z,3)..","..getBComm(z,4)..","..getBComm(z,5)..","..getBComm(z,6)..",Next,,Buy")
			elseif (p>1 and p<pages) then
				menu(id,"Commodity Trader (Sell) "..p.." - "..zones[z].name..","..getBComm(z,7)..","..getBComm(z,8)..","..getBComm(z,9)..","..getBComm(z,10)..","..getBComm(z,11)..","..getBComm(z,12)..",,Prev,Buy")
			end
		end
	elseif (pages==1) then
		menu(id,"Commodity Trader (Sell) "..p.." - "..zones[z].name..","..getBComm(z,1)..","..getBComm(z,2)..","..getBComm(z,3)..","..getBComm(z,4)..","..getBComm(z,5)..","..getBComm(z,6)..",,,Buy")
	end
end

function eqmenu(id,z,p)
	p=p or 1
	local pages=math.ceil(#basedata[z].eqtr/6)
	if (pages>1) then
		if (pages>2) then
			if (p==1) then
				menu(id,"Equipment Trader "..p.." - "..zones[z].name..","..getEq(z,1)..","..getEq(z,2)..","..getEq(z,3)..","..getEq(z,4)..","..getEq(z,5)..","..getEq(z,6)..",Next,")
			elseif (p>1 and p<pages) then
				menu(id,"Equipment Trader "..p.." - "..zones[z].name..","..getEq(z,1+(6*(p-1)))..","..getEq(z,2+(6*(p-1)))..","..getEq(z,3+(6*(p-1)))..","..getEq(z,4+(6*(p-1)))..","..getEq(z,5+(6*(p-1)))..","..getEq(z,6+(6*(p-1)))..",Next,Prev")
			else
				menu(id,"Equipment Trader "..p.." - "..zones[z].name..","..getEq(z,1+(6*(pages-1)))..","..getEq(z,2+(6*(pages-1)))..","..getEq(z,3+(6*(pages-1)))..","..getEq(z,4+(6*(pages-1)))..","..getEq(z,5+(6*(pages-1)))..","..getEq(z,6+(6*(pages-1)))..",,Prev")
			end
		elseif (pages==2) then
			if (p==1) then
				menu(id,"Equipment Trader "..p.." - "..zones[z].name..","..getEq(z,1)..","..getEq(z,2)..","..getEq(z,3)..","..getEq(z,4)..","..getEq(z,5)..","..getEq(z,6)..",Next")
			elseif (p>1 and p<pages) then
				menu(id,"Equipment Trader "..p.." - "..zones[z].name..","..getEq(z,7)..","..getEq(z,8)..","..getEq(z,9)..","..getEq(z,10)..","..getEq(z,11)..","..getEq(z,12)..",,Prev")
			end
		end
	elseif (pages==1) then
		menu(id,"Equipment Trader "..p.." - "..zones[z].name..","..getEq(z,1)..","..getEq(z,2)..","..getEq(z,3)..","..getEq(z,4)..","..getEq(z,5)..","..getEq(z,6))
	end
end

function shmenu(id,z,p)
	p=p or 1
	local pages=math.ceil(#basedata[z].shtr/6)
	if (pages>1) then
		if (pages>2) then
			if (p==1) then
				menu(id,"Equipment Trader "..p.." - "..zones[z].name..","..getShip(z,1)..","..getShip(z,2)..","..getShip(z,3)..","..getShip(z,4)..","..getShip(z,5)..","..getShip(z,6)..",Next,,Sell")
			elseif (p>1 and p<pages) then
				menu(id,"Spaceship Trader "..p.." - "..zones[z].name..","..getShip(z,1+(6*(p-1)))..","..getShip(z,2+(6*(p-1)))..","..getShip(z,3+(6*(p-1)))..","..getShip(z,4+(6*(p-1)))..","..getShip(z,5+(6*(p-1)))..","..getShip(z,6+(6*(p-1)))..",Next,Prev")
			else
				menu(id,"Spaceship Trader "..p.." - "..zones[z].name..","..getShip(z,1+(6*(pages-1)))..","..getShip(z,2+(6*(pages-1)))..","..getShip(z,3+(6*(pages-1)))..","..getShip(z,4+(6*(pages-1)))..","..getShip(z,5+(6*(pages-1)))..","..getShip(z,6+(6*(pages-1)))..",,Prev")
			end
		elseif (pages==2) then
			if (p==1) then
				menu(id,"Spaceship Trader "..p.." - "..zones[z].name..","..getShip(z,1)..","..getShip(z,2)..","..getShip(z,3)..","..getShip(z,4)..","..getShip(z,5)..","..getShip(z,6)..",Next")
			elseif (p>1 and p<pages) then
				menu(id,"Spaceship Trader "..p.." - "..zones[z].name..","..getShip(z,7)..","..getShip(z,8)..","..getShip(z,9)..","..getShip(z,10)..","..getShip(z,11)..","..getShip(z,12)..",,Prev")
			end
		end
	elseif (pages==1) then
		menu(id,"Spaceship Trader "..p.." - "..zones[z].name..","..getShip(z,1)..","..getShip(z,2)..","..getShip(z,3)..","..getShip(z,4)..","..getShip(z,5)..","..getShip(z,6)..",,")
	end
end

function findByName(name)
    for z=1,#zones do
        if (zones[z].name==name) then
            return z
        else
            return nil
        end
    end
end

function getTraders(z,m)
	if (m==1) then
		if (basedata[z] and basedata[z].commtr) then
			return "Commodities"
		end
	elseif (m==2) then
		if (basedata[z] and basedata[z].commb) then
			return "Commodities (Sell)"
		end
	elseif (m==3) then
		if (basedata[z] and basedata[z].eqtr) then
			return "Equipment"
		end
	elseif (m==4) then
		if (basedata[z] and basedata[z].shtr) then
			return "Ships"
		end
	end
	return ""
end

addhook("use","traders")
function traders(id)
    for z=1,#zones do
        if (zones[z]~=nil and zones[z].comm~=nil and player(id,"tilex")>=zones[z].comm.x1 and player(id,"tilex")<=zones[z].comm.x2 and player(id,"tiley")>=zones[z].comm.y1 and player(id,"tiley")<=zones[z].comm.y2) then
            menu(id,"TRADERS - "..zones[z].name..","..getTraders(z,1)..","..getTraders(z,2)..","..getTraders(z,3)..","..getTraders(z,4))
        end
    end
end

addhook("menu","buymenus")
function buymenus(id,menu,sel)
    for z=1,#zones do
		if (menu=="TRADERS - "..zones[z].name) then
			if (sel==1) then
				trademenu(id,z)
			end
			if (sel==2) then
				trsellmenu(id,z)
			end
			if (sel==3) then
				eqmenu(id,z)
			end
			if (sel==4) then
				shmenu(id,z)
			end
		end
		if (z and basedata[z] and basedata[z].commtr) then
			for i=1,#basedata[z].commtr do
				if (menu=="Commodity Trader "..i.." - "..zones[z].name) then
					local name=menu:sub(21)
					local page=tonumber(menu:sub(17,19))
					if (sel>=1 and sel<=6 and credits[id]>=basedata[z].commtr[(sel+(6*(page-1)))].price) then
						if (checkFree(id)) then
							insertInBackpack(id,basedata[z].commtr[(sel+(6*(page-1)))].type,1)
							credits[id]=credits[id]-basedata[z].commtr[(sel+(6*(page-1)))].price
							trademenu(id,z)
						end
					end
					if (sel==7) then
						trademenu(id,findByName(name),page+1)
					end
					if (sel==8) then
						trademenu(id,findByName(name),page-1)
					end
					if (sel==9) then
						trsellmenu(id,findByName(name))
					end
				end
			end
		end
		if (z and basedata[z] and basedata[z].commb) then
			for i=1,#basedata[z].commb do
				if (menu=="Commodity Trader (Sell) "..i.." - "..zones[z].name) then
					local name=menu:sub(30)
					local page=tonumber(menu:sub(25,26))
					if (sel>=1 and sel<=6 and findInBackpack(id,basedata[z].commb[(sel+(6*(page-1)))].type)) then
						removeFromBackpack(id,findInBackpack(id,basedata[z].commb[(sel+(6*(page-1)))].type))
						credits[id]=credits[id]+basedata[z].commb[(sel+(6*(page-1)))].price
						trsellmenu(id,z,page)
					end
					if (sel==7) then
						trsellmenu(id,findByName(name),page+1)
					end
					if (sel==8) then
						trsellmenu(id,findByName(name),page-1)
					end
					if (sel==9) then
						trademenu(id,findByName(name))
					end
				end
			end
		end
		if (z and basedata[z] and basedata[z].eqtr) then
			for i=1,#basedata[z].eqtr do
				if (menu=="Equipment Trader "..i.." - "..zones[z].name) then
					local name=menu:sub(21)
					local page=tonumber(menu:sub(17,19))
					if (sel>=1 and sel<=6 and credits[id]>=basedata[z].eqtr[(sel+(6*(page-1)))].price) then
						if (checkFree(id)) then
							insertInBackpack(id,basedata[z].eqtr[(sel+(6*(page-1)))].type+100,1)
							credits[id]=credits[id]-basedata[z].eqtr[(sel+(6*(page-1)))].price
							eqmenu(id,z)
						end
					end
					if (sel==7) then
						eqmenu(id,findByName(name),page+1)
					end
					if (sel==8) then
						eqmenu(id,findByName(name),page-1)
					end
				end
			end
		end
		if (z and basedata[z] and basedata[z].shtr) then
			for i=1,#basedata[z].shtr do
				if (menu=="Spaceship Trader "..i.." - "..zones[z].name) then
					local name=menu:sub(21)
					local page=tonumber(menu:sub(17,19))
					if (sel>=1 and sel<=6 and credits[id]>=basedata[z].shtr[(sel+(6*(page-1)))].price) then
						shipt[id]=basedata[z].shtr[(sel+(6*(page-1)))].type
						credits[id]=credits[id]-basedata[z].shtr[(sel+(6*(page-1)))].price
						initHardware(id)
					end
					if (sel==7) then
						shmenu(id,findByName(name),page+1)
					end
					if (sel==8) then
						shmenu(id,findByName(name),page-1)
					end
				end
			end
		end
    end
end
